package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.TemplateVariableAddDTO;
import org.jsola.contract.dto.TemplateVariableBatchDTO;
import org.jsola.contract.dto.TemplateVariableUpdateDTO;
import org.jsola.contract.query.TemplateVariableQuery;
import org.jsola.contract.service.ITemplateVariableService;
import org.jsola.contract.vo.TemplateVariableListVO;
import org.jsola.contract.vo.TemplateVariableVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同模板关联变量
 *
 * @author JanusMix
 */
@RestController("contractTemplateVariableController")
@RequestMapping(ContractConstants.API_PATH + "/v1/templateVariables")
public class TemplateVariableController implements ITemplateVariableControllerAPI {

    @Autowired
    private ITemplateVariableService templateVariableService;

    @Override
    @PostMapping
    public Result<TemplateVariableVO> save(@Valid @RequestBody TemplateVariableAddDTO templateVariableAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同模板关联变量权限校验

        return Result.success(
                templateVariableService.save(templateVariableAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TemplateVariableUpdateDTO templateVariableUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同模板关联变量权限校验

        int count = templateVariableService.update(templateVariableUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody TemplateVariableBatchDTO templateVariableBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<TemplateVariableAddDTO> templateVariableAddDTOList = templateVariableBatchDTO.getTemplateVariableAddDTOList();
        if (!CollectionUtils.isEmpty(templateVariableAddDTOList)) {
            templateVariableService.batchSave(templateVariableAddDTOList,tokenUser);
        }
        // 修改
        List<TemplateVariableUpdateDTO> templateVariableUpdateDTOList = templateVariableBatchDTO.getTemplateVariableUpdateDTOList();
        if (!CollectionUtils.isEmpty(templateVariableUpdateDTOList)) {
            templateVariableUpdateDTOList.parallelStream().forEach(
                    templateVariableUpdateDTO -> {
                        templateVariableService.update(templateVariableUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<Long> deleteTemplateVariableIdList = templateVariableBatchDTO.getDeleteTemplateVariableIdList();
        if (!CollectionUtils.isEmpty(deleteTemplateVariableIdList)) {
            templateVariableService.deleteByIds(tokenUser,deleteTemplateVariableIdList.toArray(new Long[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{templateVariableId}")
    public Result<Integer> delete(@PathVariable Long templateVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同模板关联变量权限校验

        int count = templateVariableService.deleteByIds(tokenUser, templateVariableId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] templateVariableIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同模板关联变量权限校验

        int count = templateVariableService.deleteByIds(tokenUser, templateVariableIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{templateVariableId}")
    public Result<TemplateVariableVO> findById(@PathVariable Long templateVariableId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同模板关联变量权限校验

        return Result.success(
            templateVariableService.selectById(templateVariableId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TemplateVariableListVO>> listAll(@Valid TemplateVariableQuery templateVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同模板关联变量权限校验

        return Result.success(
            templateVariableService.select(templateVariableQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TemplateVariableListVO>> page(@Valid TemplateVariableQuery templateVariableQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            templateVariableService.selectPage(templateVariableQuery, tokenUser.getSiteId())
        );
    }


}

